package cn.rengy.tool.excel;

import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;

/**
 * 样式创建太多会报错：
 * The maximum number of Cell Styles was exceeded. You can define up to 64000 style in a .xlsx Workbook
 * 所以需要此类
 */
public class CellStyleObject {

    private FontObject fontObject;
    private CellStyleObject(){
        fontObject=new FontObject();
    }
    public static CellStyleObject instance(){
        return new CellStyleObject();
    }

    private BorderStyle borderBottom;
    private BorderStyle borderLeft;
    private BorderStyle borderTop;
    private BorderStyle borderRight;
    private HorizontalAlignment horizontalAlignment;
    private VerticalAlignment verticalAlignment;
    private boolean wrapText;
    public FontObject getFontObject() {
        return fontObject;
    }


    /**
     * 设置label样式
     * @return
     */
    public CellStyleObject setLabelStyle(){
        setFontSize(11).setBold()
                .setAlignmentCENTER().setVerticalAlignmentCENTER()
        ;
        return this;
    }

    public CellStyleObject setBold(){
        fontObject.setBold(true);
        return this;
    }



    public BorderStyle getBorderBottom() {
        return borderBottom;
    }

    public CellStyleObject setBorderBottom(BorderStyle borderBottom) {
        this.borderBottom = borderBottom;
        return this;
    }

    public BorderStyle getBorderTop() {
        return borderTop;
    }

    public CellStyleObject setBorderTop(BorderStyle borderTop) {
        this.borderTop = borderTop;
        return this;
    }

    public BorderStyle getBorderRight() {
        return borderRight;
    }

    public CellStyleObject setBorderRight(BorderStyle borderRight) {
        this.borderRight = borderRight;
        return this;
    }

    public CellStyleObject setBorder(){
        setBorderBottom(BorderStyle.THIN); //下边框
        setBorderLeft(BorderStyle.THIN);//左边框
        setBorderTop(BorderStyle.THIN);//上边框
        setBorderRight(BorderStyle.THIN);//右边框
        return this;
    }


    public CellStyleObject setFontSize(int fontSize){
        //行高就是 fontSize*20,
        fontObject.setFontHeightInPoints((short) fontSize);//设置字体大小
        return this;
    }


    private FillPatternType fillPattern;

    private HSSFColor.HSSFColorPredefined fillForegroundColor;

    public FillPatternType getFillPattern() {
        return fillPattern;
    }

    public CellStyleObject setFillPattern(FillPatternType fillPattern) {
        this.fillPattern = fillPattern;
        return this;
    }

    public HSSFColor.HSSFColorPredefined getFillForegroundColor() {
        return fillForegroundColor;
    }

    public CellStyleObject setFillForegroundColor(HSSFColor.HSSFColorPredefined fillForegroundColor) {
        this.fillForegroundColor = fillForegroundColor;
        return this;
    }

    /**
     * 设置前景色-黄
     * @return
     */
    public CellStyleObject setForegroundColorYELLOW(){
        setFillForegroundColor(HSSFColor.HSSFColorPredefined.YELLOW);
        setFillPattern(FillPatternType.SOLID_FOREGROUND);
        return this;
    }


    /**
     * 设置正文样式
     * @return
     */
    public CellStyleObject setBodyStyle(){
        setFontSize(11).setAlignmentLEFT().setVerticalAlignmentCENTER().setWrapText();
        return this;
    }


    public boolean isWrapText() {
        return wrapText;
    }

    public CellStyleObject setWrapText(boolean wrapText) {
        this.wrapText = wrapText;
        return this;
    }

    /**
     * 设置自动换行
     * @return
     */
    public CellStyleObject setWrapText(){
        setWrapText(true);//自动换行
        return this;
    }



    public BorderStyle getBorderLeft() {
        return borderLeft;
    }

    public CellStyleObject setBorderLeft(BorderStyle borderLeft) {
        this.borderLeft = borderLeft;
        return this;
    }

    public CellStyleObject setBorderLeftNone(){
        setBorderLeft(BorderStyle.NONE);//左边框
        return this;
    }




    public HorizontalAlignment getHorizontalAlignment() {
        return horizontalAlignment;
    }

    public CellStyleObject setHorizontalAlignment(HorizontalAlignment horizontalAlignment) {
        this.horizontalAlignment = horizontalAlignment;
        return this;
    }

    /**
     * 水平居中
     * @return
     */
    public CellStyleObject setAlignmentCENTER(){
        setHorizontalAlignment(HorizontalAlignment.CENTER);//水平居中
        return this;
    }

    /**
     * 水平居左
     * @return
     */
    public CellStyleObject setAlignmentLEFT(){
        setHorizontalAlignment(HorizontalAlignment.LEFT);//水平居左
        return this;
    }

    /**
     * 水平居右
     * @return
     */
    public CellStyleObject setAlignmentRIGHT(){
        setHorizontalAlignment(HorizontalAlignment.RIGHT);//水平居右
        return this;
    }



    public VerticalAlignment getVerticalAlignment() {
        return verticalAlignment;
    }

    public CellStyleObject setVerticalAlignment(VerticalAlignment verticalAlignment) {
        this.verticalAlignment = verticalAlignment;
        return this;
    }

    /**
     * 垂直居中
     * @return
     */
    public CellStyleObject setVerticalAlignmentCENTER(){
        setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
        return this;
    }
}
